package Fenetres;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class ListScenarioFiles extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private final DefaultListModel<File> model = new DefaultListModel<>();

	/**
	 * Create the frame.
	 */
	public ListScenarioFiles() {
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel panel_center = new JPanel();
		contentPane.add(panel_center, BorderLayout.CENTER);
		panel_center.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		panel_center.add(scrollPane, BorderLayout.CENTER);

		JList<File> list_Files = new JList<File>(model);
		list_Files.setBorder(null);
		scrollPane.setViewportView(list_Files);

		JPanel panel_buttom = new JPanel();
		contentPane.add(panel_buttom, BorderLayout.SOUTH);
		panel_buttom.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));

		JButton btnAnnuler = new JButton("Annuler");
		panel_buttom.add(btnAnnuler);

		JButton btnConsulter = new JButton("Consulter");
		btnConsulter.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showScenario();
			}
		});

		panel_buttom.add(btnConsulter);

		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel, BorderLayout.NORTH);

		JLabel label = new JLabel("Consulter les fichiers sc\u00E9narii");
		label.setHorizontalAlignment(SwingConstants.LEFT);
		label.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel.add(label);

		// load files
		loadFiles();
	}

	/**
	 * 
	 */
	public void loadFiles() {
		String dir = "..\\..\\BackOffice\\files\\";
		File file = new File(dir);
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].getName().endsWith(".xml")) {
					model.addElement(files[i]);
				}
			}
		}
	}

	public void showScenario() {
		ConsultScenario consult = new ConsultScenario(this);
		consult.setVisible(true);
	}
}
